
package com.glela.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestUserBankModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.service.UserBankService;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping(value = "/userBank")
@Scope("singleton")
public class UserBankController extends BaseController {

	@Autowired
	private UserBankService userBankService;
	
	/**
	 * 获取合作的银行列表
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value = "/getBankList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getBankList(){
		return userBankService.getBankLst();
	}
	
	/**
	 * 查询用户的银行卡列表
	 * @param userModel
	 * @return
	 */
	@RequestMapping(value = "/getBankCardList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getBankCardList(@RequestBody RequestUserBankModel model){
		if (ParameterUtils.validParams(model.getUserId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return userBankService.listUserBankPage(model.getUserId(), model.getStartIndex(), model.getPageSize());
	}
	
	/**
	 * 删除银行卡
	 * @param userModel
	 * @param pages
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/deleteBankCard", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> deleteBankCard(@RequestBody RequestUserBankModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(), model.getBankCardId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return userBankService.removeUserBankById(model.getUserId(), model.getBankCardId());
	}
	
	/**
	 * 新增银行卡
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addBankCard", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addBankCard(@RequestBody RequestUserBankModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(), model.getName(),model.getBankCard(),model.getBankId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return userBankService.addUserBank(model.getUserId(), model.getName(),model.getBankCard(),model.getBankId(),model.getBankBranchName(),model.getBankBranchId(),model.getIdNumber());
	}
	
	/**
	 * 查询用户的默认银行卡以及余额
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getBankCardAndMoney", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getBankCardAndMoney(@RequestBody RequestUserBankModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return userBankService.getUserBankAndMoney(model.getUserId());
	}
	
	/**
	 * 用户提现
	 * @param model
	 * @return
	 * @author gaoweiqi
	 * @date 2017年8月21日 12:44:05
	 * @throws Exception
	 */
	@RequestMapping(value = "/userDrawings", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> userDrawings(@RequestBody RequestUserBankModel model) throws Exception{
		
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return userBankService.userDrawings(model.getUserId(), model.getMoney(), model.getBankCardId());
		} catch (Exception e) {
			getLOGGER().error("tixianyichang :{}  :", e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
		
	}
	
	/**
	 * 查询支付宝账号
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userAliInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> userAliInfo(@RequestBody RequestUserBankModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return userBankService.getUserBankAli(model.getUserId());
	}
	
}
